package com.itheima.server.controller;

import com.itheima.server.service.CommentService;
import com.itheima.server.service.MovementsService;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Visitors;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import org.hibernate.validator.constraints.EAN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private CommentService commentService;

    /**
     *接口名称：动态-发布
     *接口路径：POST/movements
     */
    @PostMapping
    public ResponseEntity saveMovements(Movement movement,MultipartFile[] imageContent) throws IOException {
        movementsService.saveMovements(movement,imageContent);
        return ResponseEntity.ok(null);
    }


    /**
     *接口名称：根据用户id查询当前用户的动态
     *接口路径：GET/movements/all
     */
    @GetMapping("/all")
    public ResponseEntity findByUserId(Integer page,Integer pagesize,Long userId) {
        PageResult pageResult = movementsService.findByUserId(page,pagesize,userId);
        return ResponseEntity.ok(pageResult);
    }


    /**
     *接口名称：好友动态
     *接口路径：GET/movements
     */
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementsService.findFriendMovements(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     *接口名称：推荐动态
     *接口路径：GET/movements/recommend
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementsService.recommend(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }


    /**
     *接口名称：单条动态
     *接口路径：GET/movements/:id
     */
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String movementId) {
        MovementsVo movementsVo = movementsService.findById(movementId);
        return ResponseEntity.ok(movementsVo);
    }

    /**
     *接口名称：评论-点赞
     *接口路径：GET/comments/:id/like
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId){
        Integer likeCount = commentService.likeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }


    /**
     *接口名称：动态-取消点赞
     *接口路径：GET/movements/:id/dislike
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId){
        Integer likeCount = commentService.dislikeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }


    /**
     * 喜欢
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.loveComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消喜欢
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.disloveComment(movementId);
        return ResponseEntity.ok(likeCount);
    }


    /**
     *接口名称：谁看过我
     *接口路径：GET/movements/visitors
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors() {
        List<VisitorsVo> voList = movementsService.queryVisitorsList();
        return ResponseEntity.ok(voList);
    }
}